package com.gexiaobao.pigeon.ui.pigeoncatcher

import android.annotation.SuppressLint
import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityPigeonCatcherInfoBinding
import com.gexiaobao.pigeon.viewmodel.PigeonCatcherViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/6/24 14:11
 * @Describe    : 接鸽员个人信息
 */
class ActivityPigeonCatcherInfo : BaseActivity<PigeonCatcherViewModel, ActivityPigeonCatcherInfoBinding>() {

    private var orgId = 0

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "个人中心"

        orgId = intent.getIntExtra("orgId", 0)

        showLoading(getString(R.string.loading))
        mViewModel.getTransporterApplyBaseByUserId()
        mViewModel.getTransporterApplyCarList()

        mDatabind.swipeRefreshLayout.init {
            mViewModel.getTransporterApplyBaseByUserId()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun createObserver() {
        super.createObserver()
        /** 查询接鸽员申请中关于汽车的信息列表  */
        mViewModel.carInfoListResult.observe(this) {
            if (it.list.isNotEmpty()) {
                val licenseNumber = it.list[0].licensePlateNumber
                mDatabind.tvLicenseNumber.text = licenseNumber
            }
        }
        /** 查询接鸽员申请中关于个人的信息查询（用户角色）  */
        mViewModel.drippingBaseInfoResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            if (it != null) {
                val identityCard = it.identityCard
                if (identityCard.isNotEmpty() && identityCard.length >= 18) {
                    val result = identityCard.substring(0, 3)
                    val result2 = identityCard.substring(14, identityCard.length)
                    mDatabind.tvIdentityCard.text = "$result***********$result2"
                }
                mDatabind.data = it
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.llOrderFinish, mDatabind.llLicenseNumber) {
            when (it) {
                mDatabind.llLicenseNumber -> {
                    startActivity<ActivityCarInfo>("orgId" to orgId)
                }

                mDatabind.llOrderFinish -> {
                    startActivity<ActivityHistoryOrderDripping>("orgId" to orgId)
                }

                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }
}